using System.Collections;
using System.Collections.Generic;
using ProjectSurvivor;
using QFramework;
using QAssetBundle;
using Unity.VisualScripting;
using UnityEngine;

public class CoinUpgradeSystem : AbstractSystem, IcanSave
{
    public static EasyEvent OnCoinUpgradeSystemChange = new EasyEvent();
    
    public List<CoinUpgradeItem> Items = new List<CoinUpgradeItem>();

    public CoinUpgradeItem Add(CoinUpgradeItem item)
    {
        Items.Add(item);
        return item;
    }
    
    protected override void OnInit()
    {
        Add(new CoinUpgradeItem()
            .WithKey("coin_percent_lv1_")
            .WithDescription($"提升金币掉落概率 Lv1")
            .WithPrice(5)
            .OnUpgrade((item) =>
            {
                AudioKit.PlaySound(Sfx.ABILITYLEVELUP);
                Global.CoinPercent.Value += 0.1f;
                Global.Coin.Value -= item.Price;
            })
            .Next(Add(new CoinUpgradeItem()
                .WithKey("coin_percent_lv2_")
                .WithDescription($"提升金币掉落概率 Lv2")
                .WithPrice(7)
                .OnUpgrade((item) =>
                {
                    AudioKit.PlaySound(Sfx.ABILITYLEVELUP);
                    Global.CoinPercent.Value += 0.1f;
                    Global.Coin.Value -= item.Price;
                })
                .Next(Add(new CoinUpgradeItem()
                    .WithKey("coin_percent_lv3_")
                    .WithDescription($"提升金币掉落概率 Lv3")
                    .WithPrice(10)
                    .OnUpgrade((item) =>
                    {
                        AudioKit.PlaySound(Sfx.ABILITYLEVELUP);
                        Global.CoinPercent.Value += 0.1f;
                        Global.Coin.Value -= item.Price;
                    }))))));
        
        Items.Add(new CoinUpgradeItem()
            .WithKey("Exp_percent_")
            .WithDescription($"提升经验掉落概率")
            .WithPrice(5)
            .OnUpgrade((item) =>
            {
                AudioKit.PlaySound(Sfx.ABILITYLEVELUP);
                Global.ExpPercent.Value += 0.1f;
                Global.Coin.Value -= item.Price;
            }));
        
        Items.Add(new CoinUpgradeItem()
            .WithKey("max_hp_")
            .WithDescription($"主角最大血量+1")
            .WithPrice(30)
            .OnUpgrade((item) =>
            {
                AudioKit.PlaySound(Sfx.ABILITYLEVELUP);
                Global.MaxHP.Value++;
                Global.Coin.Value -= item.Price;
            }));
        
        // 加载配置
        Load();
        
        // 监听事件，保存配置
        OnCoinUpgradeSystemChange.Register(() =>
        {
            Save();
        });
    }

    public void Save()
    {
        var saveSystem = this.GetSystem<SaveSystem>();
        foreach (var coinUpgradeItem in Items)
        {
            saveSystem.SaveBool(coinUpgradeItem.Key, coinUpgradeItem.UpgradeFinish);
        }
    }

    public void Load()
    {
        var saveSystem = this.GetSystem<SaveSystem>();
        foreach (var coinUpgradeItem in Items)
        {
            coinUpgradeItem.UpgradeFinish = saveSystem.LoadBool(coinUpgradeItem.Key);
        }
    }
}
